﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
namespace PTHTTT_STDT_Demo
{
    class Connect
    {
        public string Server { get; set; }
        public string Database { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        // phuong thuc lay thuoc tinh tu file app.config
        public Connect()
        {
            this.Server = ConfigurationManager.AppSettings.Get("Server");
            this.Database = ConfigurationManager.AppSettings.Get("Database");
            this.Username = ConfigurationManager.AppSettings.Get("Username");
            this.Password = ConfigurationManager.AppSettings.Get("Password");
        }
        //phuong thuc tĩnh của lớp ghi thuoc tinh vao file app.config
        public static void saveConfig(string key, string value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);
            config.AppSettings.Settings[key].Value = value;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        //phuong thuc ket noi den co so du lieu o day la master
        public SqlConnection get_Connect()
        {
            if (this.Username != "")
            {
                return new SqlConnection("Data Source=" + this.Server + ";Initial Catalog=" + this.Database + ";User Id=" + this.Username + ";Password=" + this.Password + ";");
            }
            else
                return new SqlConnection("Data Source=" + this.Server + ";Initial Catalog=" + this.Database + ";Integrated Security=True");
        }
    }
}
